import * as api from '@/api'

export default {
    namespaced:true,
    state:{
        products:[]
    },
    mutations:{
        PRODUCT(state,payload){
            state.products = payload
        },
        //库存减少
        DEL_INVENTORY(state,payload){
            let target = state.products.find(item=>item.id===payload.id)
           if(target){
             target.inventory --
           }
        },
        //库存增加
        ADD_INVENTORY(state,payload){
            let target = state.products.find(item=>item.id===payload.id)
            if(target){
              target.inventory ++
            }
        }
    },
    actions:{
        //请求的方法
        async FETCH_PRODUCT(context,payload){
            let res = await api.fetchProduct()
            console.log(res)
            context.commit('PRODUCT',res.data)

        },
        //加入的方法
        ADD_CART(context,payload){
            context.commit('cart/ADD_CARTS',payload,{root:true})
            context.commit('DEL_INVENTORY',payload)
        },
       

    },
    getters:{}
}